package demo.qc.viewflowerguide;

import org.taptwo.android.widget.FlowIndicator;
import org.taptwo.android.widget.ViewFlow;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class BitMapIndicator extends View implements FlowIndicator{
	private static final String TAG = "BitMapIndicator";
	private int flowWidth = 0;
	
	private final Paint mLinePaint= new Paint(Paint.ANTI_ALIAS_FLAG);
	private float lineHeight=3;
	private int lineColor=0x901d22;
	private int currentScroll = 0;
	private int count;
	
	public BitMapIndicator(Context context) {
		super(context);
		initColors(lineColor,lineHeight);
	}
	
	public BitMapIndicator(Context context,int lineColor,int lineHeight,int flowWidth) {
		super(context);
		initColors(lineColor,lineHeight);
		this.flowWidth = flowWidth;
	}

	public BitMapIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		/*TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.LineFlowIndicator);
		int defaultLineColor = 0x901d22;
		lineColor = a .getColor(R.styleable.LineFlowIndicator_lineFlowColor,defaultLineColor);
		lineHeight = a.getDimension(R.styleable.LineFlowIndicator_lineFlowHeight, 1);*/
	/*	a.recycle();*/
	      initColors(lineColor,lineHeight);
	}
	
	private void initColors(int lineColor,float lineHeight) {
		mLinePaint.setColor(lineColor);
		mLinePaint.setStyle(Paint.Style.FILL);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Log.v(TAG, "onDraw()");
		
		float cx = 0;
		float lineWidth = flowWidth/count;
		if (flowWidth != 0) {
			// Draw the filled circle according to the current scroll
			cx =lineWidth *currentScroll;//(currentScroll ) / flowWidth;
		}
		// The flow width has been upadated yet. Draw the default position
		canvas.drawRect(cx, 0, lineWidth+cx,getMeasuredHeight(), mLinePaint);
		Log.d(TAG, "cx="+cx+",flowWidth="+flowWidth+",flowWidth/count="+flowWidth/count+",getMeasuredHeight()="+getMeasuredHeight());
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		Log.i(TAG, "onMeasure()");
		setMeasuredDimension(measureWidth(widthMeasureSpec),
				measureHeight(heightMeasureSpec));
	}
	
	/**
	 * Determines the width of this view
	 * 
	 * @param measureSpec
	 *            A measureSpec packed into an int
	 * @return The width of the view, honoring constraints from measureSpec
	 */
	private int measureWidth(int measureSpec) {
	 	int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		// We were told how big to be
		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
		}
		// Calculate the width according the views count
		else {
			if (count != 0) {
				result = flowWidth / count;
			}
			// Respect AT_MOST value if that was what is called for by
			// measureSpec
			if (specMode == MeasureSpec.AT_MOST) {
				result = Math.min(result, specSize);
			}
		} 
		return specSize;
	}
	
	/**
	 * Determines the height of this view
	 * 
	 * @param measureSpec
	 *            A measureSpec packed into an int
	 * @return The height of the view, honoring constraints from measureSpec
	 */
	private int measureHeight(int measureSpec) {
		
 	int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		// We were told how big to be
		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
		}
		// Measure the height
		else {
			result = (int) (lineHeight);
			// Respect AT_MOST value if that was what is called for by
			// measureSpec
			if (specMode == MeasureSpec.AT_MOST) {
				result = Math.min(result, specSize);
			}
		} 
		return result;
	}
	
	public void setCount(int count){
		this.count = count;
	}
	
	public void setCurScroll(int curScroll){
		this.currentScroll = curScroll;	
		invalidate();
	}

	@Override
	public void onSwitched(View view, int position) {
		
	}

	@Override
	public void setViewFlow(ViewFlow view) {
		
	}

	@Override
	public void onScrolled(int h, int v, int oldh, int oldv) {
		
	}


}
